package com.cloudwise.monitoring_system.util;

import org.springframework.stereotype.Component;
import redis.clients.jedis.HostAndPort;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Reisen
 * @description:获取redis集群链接
 * @date 2021/6/7 11:14
 */
@Component
public class RedisClusterConnection {
    @Resource
    private RedisProperties redisProperties;

    /**
     * 获取redis集群的ip和端口号
     * @return
     */
    public Set<HostAndPort> getRedisClusters(){
        Set<HostAndPort> nodesSet = new HashSet<>();
        //redis.nodes=192.168.84.101:6001,192.168.84.101:6002...类似于这样，进行字符串拆分
        String[] nodesArray = redisProperties.getNodes().split(",");
        for (String node : nodesArray) {
            //node=192.168.84.101:6001,再次拆分得到IP和port
            String[] hostAndport = node.split(":");
            HostAndPort hostAndPort = new HostAndPort(hostAndport[0], Integer.valueOf(hostAndport[1]));
            nodesSet.add(hostAndPort);
        }
        //System.out.println(nodesSet.toString());
        return nodesSet;
    }
}
